/**
 * 
 */
package com.supereye.platform.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 
 * @author zhangxh
 *
 */
public class ExcelUtil {
	public static void main(String[] args) throws Exception {
		List<Map<Integer,Object>> list = ExcelUtil.readExcel("C:\\topeye\\upload\\tmp\\1484635663471_usertemp.xlsx");
		System.out.println(list);
	}
	
	/**
	 * 读取EXCEL
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static List<Map<Integer,Object>> readExcel(String path) throws Exception {
		if(path.toLowerCase().endsWith(".xlsx")){
			return readXlsx(path);
		}else if(path.toLowerCase().endsWith(".xls")){
			return readXls(path);
		}else{
			return null;
		}
	}

    /**
     * Read the Excel 2010
     * @param path the path of the excel file
     * @return
     * @throws IOException
     */
    public static List<Map<Integer,Object>> readXlsx(String path) throws Exception {
    	List<Map<Integer,Object>> list = new ArrayList<Map<Integer,Object>>();
        InputStream is = new FileInputStream(path);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
        Map<Integer,Object> map = null;
        for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
            XSSFRow xssfRow = xssfSheet.getRow(rowNum);
            int columns = xssfRow.getLastCellNum();
            map = new HashMap<Integer,Object>();
            for(int i=0;i<columns;i++){
            	XSSFCell cell = xssfRow.getCell(i);
            	map.put(i, getValue(cell));
            }
            list.add(map);
        }
        xssfWorkbook.close();
        is.close();
        return list;
    }

    /**
     * Read the Excel 2003-2007
     * @param path the path of the Excel
     * @return
     * @throws IOException
     */
    public static List<Map<Integer,Object>> readXls(String path) throws Exception {
        InputStream is = new FileInputStream(path);
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        Map<Integer,Object> map = null;
        List<Map<Integer,Object>> list = new ArrayList<Map<Integer,Object>>();
        HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
        for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
            HSSFRow hssfRow = hssfSheet.getRow(rowNum);
            int columns = hssfRow.getLastCellNum();
            map = new HashMap<Integer,Object>();
            for(int i=0;i<columns;i++){
            	HSSFCell cell = hssfRow.getCell(i);
            	map.put(i, getValue(cell));
            }
            list.add(map);
        }
        hssfWorkbook.close();
        is.close();
        return list;
    }

    private static String getValue(XSSFCell xssfRow) {
    	if(xssfRow!=null){
    		if (xssfRow.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
                return String.valueOf(xssfRow.getBooleanCellValue());
            } else if (xssfRow.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                return String.valueOf(xssfRow.getNumericCellValue());
            } else {
                return String.valueOf(xssfRow.getStringCellValue());
            }
    	}else{
    		return null;
    	}
        
    }

    private static String getValue(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
            return String.valueOf(hssfCell.getNumericCellValue());
        } else {
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }
}